/*
 * Created on 09.09.2006 by leck
 *
 */
package de.devisnik.eidle.picture;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;

import junit.framework.TestCase;
import de.devisnik.eidle.picture.FileProvider;
import de.devisnik.eidle.picture.IFileProvider;

public class FileProviderTest extends TestCase {

    private IFileProvider itsFileProvider;
    private final static File FILE1 = new File("files/file1.test");
    private final static File FILE2 = new File("files/file2.test");
    private final static File FILE3 = new File("files/file3.test.ext");
    private static final File DIRECTORY = new File("files");
    
    public FileProviderTest() {
        super();
    }

//    @Override
    protected void setUp() throws Exception {
        super.setUp();
        itsFileProvider = new FileProvider(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".test");
            }
        });
    }

    public void testGetSize() throws FileNotFoundException {
        int numberOfAddedFiles = addSomeFiles(itsFileProvider);
        assertEquals(itsFileProvider.getSize(), numberOfAddedFiles);
    }

    public void testGetNextPath() throws FileNotFoundException {
        File currentFile = itsFileProvider.getNext();
        assertNull(currentFile);

        addSomeFiles(itsFileProvider);
        currentFile = itsFileProvider.getNext();
        assertEquals(FILE1, currentFile);
        currentFile = itsFileProvider.getNext();
        assertEquals(FILE2, currentFile);
        currentFile = itsFileProvider.getNext();
        assertNull(currentFile);
    }

    private int addSomeFiles(IFileProvider filePathProvider) throws FileNotFoundException {
        filePathProvider.addResource(FILE1);
        filePathProvider.addResource(FILE2);
        filePathProvider.addResource(FILE3);
        int numberOfAddedFiles = 2;
        return numberOfAddedFiles;
    }

    public void testAddResource() {
        try {
            addSomeFiles(itsFileProvider);
            addExistingDir(itsFileProvider);
        }
        catch (FileNotFoundException e1) {
            fail();
        }
        try {
            itsFileProvider.addResource(new File("noexisting.resource"));
        }
        catch (FileNotFoundException e) {
            return;
        }
        fail();
    }
        
    private void addExistingDir(IFileProvider fileProvider) throws FileNotFoundException {
        fileProvider.addResource(DIRECTORY);
    }

    public void testReset() throws FileNotFoundException {
        itsFileProvider.reset();
        File nextPath = itsFileProvider.getNext();
        assertNull(nextPath);

        addSomeFiles(itsFileProvider);
        itsFileProvider.getNext();
        itsFileProvider.getNext();
        itsFileProvider.getNext();
        itsFileProvider.getNext();

        itsFileProvider.reset();
        nextPath = itsFileProvider.getNext();
        assertEquals(FILE1, nextPath);

    }
    
    
    public void testHasNext() throws FileNotFoundException {
        assertFalse(itsFileProvider.hasNext());
        
        int addedFiles = addSomeFiles(itsFileProvider);
        assertTrue(itsFileProvider.hasNext());
        for (int i = 0; i < addedFiles-1; i++) {
            itsFileProvider.getNext();
            assertTrue(itsFileProvider.hasNext());
        }
        itsFileProvider.getNext();
        assertFalse(itsFileProvider.hasNext());
    }
}
